package com.soft.collateral.controller;

import com.soft.collateral.constant.MessageConstant;
import com.soft.collateral.entity.User;
import com.soft.collateral.exception.RefreshTokenInvalidException;
import com.soft.collateral.result.Result;
import com.soft.collateral.service.UserService;
import com.soft.collateral.utils.JwtHelper;
import com.soft.collateral.vo.UserLoginQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/7/24
 */
@RestController
@RequestMapping("/admin/auth")
@Slf4j
@Api(tags = "后台系统-认证接口")
@RequiredArgsConstructor
public class AuthController {

    private final UserService userService;

    @ApiOperation("登录")
    @PostMapping("login")
    public Result login(@RequestBody UserLoginQueryVO vo){
        //1.通过姓名和密码查询用户信息
        User user = userService.login(vo);
        if(!ObjectUtils.isEmpty(user)){
            //2.姓名和密码匹配时，生成token令牌
            //生成访问Token令牌
            String accessToken = JwtHelper.createToken(user.getId().longValue(), user.getUsername());
            //生成刷新Token令牌
            String refreshToken = JwtHelper.refreshToken(accessToken);
            Map<String, Object> map = new HashMap<>();
            map.put("accessToken", accessToken);
            map.put("refreshToken", refreshToken);
            map.put("expires",System.currentTimeMillis() + JwtHelper.getTokenExpiration());
            //3.响应token信息
            return new Result(true, MessageConstant.LOGIN_SUCCESS, map);
        }
        return new Result(false , MessageConstant.LOGIN_FAIL);
    }

    @ApiOperation("刷新令牌")
    @PostMapping("refreshToken")
    public Result refreshToken(@RequestBody Map<String,String> refreshTokenMap){
        String refreshToken = refreshTokenMap.get("refreshToken");
        if(!StringUtils.hasLength(refreshToken) || JwtHelper.isExpiration(refreshToken)){
            throw new RefreshTokenInvalidException(MessageConstant.REFRESH_TOKEN_VERIFY_FAIL);
        }

        //获取刷新令牌中的登录信息
        Long userId = JwtHelper.getUserId(refreshToken);
        String userName = JwtHelper.getUserName(refreshToken);
        //重新生成访问accessToken令牌
        String accessToken = JwtHelper.createToken(userId, userName);
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", accessToken);
        map.put("refreshToken", refreshToken);
        map.put("expires",System.currentTimeMillis() + JwtHelper.getTokenExpiration());
        //3.响应token信息
        return new Result(true, MessageConstant.TOKEN_REFRESH, map);
    }

    @ApiOperation("登出")
    @GetMapping("logout")
    public Result logout(HttpSession session){
        session.invalidate();
        return new Result(true , MessageConstant.LOGOUT_SUCCESS);
    }
}